package org.pearl.springbootsecurity.demo.config;

import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.StringValue;

import java.util.Random;

/**
 * Created by TD on 2021/9/3
 */
@Slf4j
public class MyTenantLineHandler implements TenantLineHandler {


    /**
     * 获取租户ID 实际应该从用户信息中获取
     *
     * @return
     */
    @Override
    public Expression getTenantId() {
        // 模拟ID
        log.info("==========================getTenantId");
        String userTenantId = "000" + (new Random().nextInt(2) + 1);
        return new StringValue(userTenantId);
    }

    /**
     * 获取租户表字段 默认为tenant_id
     *
     * @return
     */
    @Override
    public String getTenantIdColumn() {
        log.info("==========================getTenantIdColumn");
        return "tenant_id";
    }

    /**
     * 表过滤，返回true，表示当前表不进行租户过滤
     *
     * @param tableName 表名
     * @return
     */
    @Override
    public boolean ignoreTable(String tableName) {
        // 排除user表
        log.info("==========================ignoreTable");
        return "user_role".equalsIgnoreCase(tableName);
    }
}


